// -------------------------------------------------
// BlinKit - BlinKit Library
// -------------------------------------------------
//   File Name: qjs_keyboard_event.cpp
// Description: Bindings for KeyboardEvent
//      Author: Ziming Li
//     Created: 2022-10-01
// -------------------------------------------------
// Copyright (C) 2022 MingYang Software Technology.
// -------------------------------------------------

#include "./qjs_bindings.h"

#include "blinkit/blink/renderer/bindings/core/qjs/qjs.h"
#include "blinkit/blink/renderer/bindings/core/qjs/qjs_c_function_list_entries.h"
#include "blinkit/blink/renderer/bindings/core/script_controller.h"
#include "blinkit/blink/renderer/core/events/KeyboardEvent.h"

/**
 * https://developer.mozilla.org/en-US/docs/Web/API/KeyboardEvent
 */

namespace BlinKit {
namespace qjs {

static JSValue GetCode(JSContext *ctx, JSValueConst thisVal)
{
    KeyboardEvent *event = ScriptWrappable::Cast<KeyboardEvent>(thisVal);
    return WrapString(ctx, event->code());
}

static JSValue GetKey(JSContext *ctx, JSValueConst thisVal)
{
    KeyboardEvent *event = ScriptWrappable::Cast<KeyboardEvent>(thisVal);
    return WrapString(ctx, event->key());
}

static JSValue GetKeyCode(JSContext *ctx, JSValueConst thisVal)
{
    KeyboardEvent *event = ScriptWrappable::Cast<KeyboardEvent>(thisVal);
    return JS_NewInt32(ctx, event->keyCode());
}

JSValue CreateKeyboardEventPrototype(JSContext *ctx, JSValue uiEventPrototype)
{
    static const JSCFunctionListEntry Funcs[] = {
        CGetSetEntry("code", GetCode, nullptr),
        CGetSetEntry("key", GetKey, nullptr),
        CGetSetEntry("keyCode", GetKeyCode, nullptr)
    };

    JSValue ret = JS_NewObjectProto(ctx, uiEventPrototype);
    JS_SetPropertyFunctionList(ctx, ret, Funcs, std::size(Funcs));
    return ret;
}

} // namespace qjs
} // namespace BlinKit
